package org.kabeja.parser.dxf.filter;

import org.kabeja.parser.DXFValue;
import org.kabeja.parser.ParseException;

/**
 * @author shutao
 * @since 2022/4/25 10:56
 */
abstract class DXFStreamEntityFilter extends DXFStreamSectionFilter {
    private static String SECTION_KEY = "ENTITIES";
    public static final int ENTITY_START = 0;
    protected boolean entitySection = false;
    protected boolean parseEntity = false;
    protected boolean parseHeader = false;

    DXFStreamEntityFilter() {
    }

    @Override
    protected void parseSection(int var1, DXFValue var2) throws ParseException {
        if (this.parseHeader) {
            if (var2.getValue().equals(SECTION_KEY)) {
                this.entitySection = true;
                this.parseHeader = false;
            }
        } else if (this.entitySection) {
            if (var1 == ENTITY_START) {
                if (this.parseEntity) {
                    this.endEntity();
                } else {
                    this.parseEntity = true;
                }

                this.startEntity(var2.getValue());
            }

            this.parseEntity(var1, var2);
            return;
        }

        this.handler.parseGroup(var1, var2);
    }

    @Override
    protected void sectionEnd(String var1) throws ParseException {
        if (this.section.equals(SECTION_KEY)) {
            this.entitySection = false;
        }

    }

    @Override
    protected void sectionStart(String var1) throws ParseException {
        if (this.section.equals(SECTION_KEY)) {
            this.parseHeader = true;
        }

    }

    protected abstract void startEntity(String var1) throws ParseException;

    protected abstract void endEntity() throws ParseException;

    protected abstract void parseEntity(int var1, DXFValue var2) throws ParseException;
}
